<template>
  <el-dialog title="更换师训" :close-on-click-modal="false" :visible.sync="visible" class="cu-dialog cu-dialog_center" lock-scroll width="1000px">
    <el-row :gutter="15" class="">
      <el-form ref="elForm" :model="dataForm" size="small" label-width="100px" label-position="right" :rules="rules">
        <el-col :span="12">
          <el-form-item label="师训项目" prop="trainId">
            <el-select v-model="dataForm.trainId" placeholder="请选择" clearable required filterable :style="{ width: '100%' }">
              <el-option v-for="(item, index) in trainOptions" :key="index" :label="item.title" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog()">取 消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import request from '@/utils/request'
import { changeNewTrain, getTrainNoPagingList } from '@/api/campus/train'
export default {
  props: [],
  data() {
    return {
      loading: false,
      visible: false,
      dataForm: {
        id: '',
        trainId: undefined
      },
      rules: {
        objectId: [
          {
            required: true,
            message: '请选择师训项目',
            trigger: 'blur'
          }
        ]
      },
      trainOptions: []
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
    getTrainNoPagingList().then(res => {
      this.trainOptions = res.data
    })
  },
  methods: {
    goBack() {
      this.$emit('refresh')
    },
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['elForm'].resetFields()
        if (this.dataForm.id) {
          request({
            url: '/api/train/sign/' + this.dataForm.id,
            method: 'get'
          }).then(res => {
            this.dataForm = res.data
          })
        }
      })
    },
    dataFormSubmit() {
      this.$refs['elForm'].validate(valid => {
        if (valid) {
          changeNewTrain(this.dataForm).then(res => {
            this.$message({
              message: res.msg,
              type: 'success',
              duration: 1000,
              onClose: () => {
                this.$emit('refresh', true)
                this.closeDialog()
              }
            })
          })
        }
      })
    },
    closeDialog() {
      this.visible = false
      this.$refs['elForm'].resetFields()
    }
  }
}
</script>
